package lib.Com;


import java.io.*;
import java.net.*;

import lib.gui.GuiMain;

public class Client extends Thread {
	/**
	 * The client class represents a client which wants to send data objects.
	 */
	
	private Socket clientSocket = new Socket();
    private OutputStream output;
    private KnownClient currClient = null;
    private Object toSend = null;
	
    public Client(KnownClient currClient, Object toSend){
    	super("TCPClient");
    	this.currClient = currClient;
    	this.toSend = toSend;
    	try {
    		clientSocket.setSoTimeout(300);
		} catch (Exception e) {
		}
    }
    
    public void run() {
    	if(currClient!=null && toSend!=null) {
    		try {
    			clientSocket.connect(new InetSocketAddress(currClient.getIPlocal(), currClient.getPort1()));
			} catch (Exception e) {
				try {
					clientSocket.connect(new InetSocketAddress(currClient.getIPpublic(), currClient.getPort1()));
				} catch (Exception e2) {
					Container.knownClients.remove(currClient.getID());
					GuiMain.removeFriend(currClient.getID());
					return;
				}
			}
    		try {
	    		output = clientSocket.getOutputStream();
	    		ObjectOutputStream oos = new ObjectOutputStream(output);  
	    		oos.writeObject(toSend);  
	
	    		oos.close();
	    		output.close();
	    		clientSocket.close();
    		}catch(Exception e3) {
    		}
    	}
    }
}